#!/usr/bin/env python

import sys
import os.path
import warnings
import getopt
from datetime import date
from pprint import PrettyPrinter

import suds.client
import inspect

filetype = 'FASTQ'
wsdlUrl = 'http://uk-cri-ldmz02/solexa-ws/SolexaExportBeanWS?wsdl'

###############################################################################

def processOptions(argv):
  global filetype
  try:
    (opts,args) = getopt.gnu_getopt(argv[1:],"f:",['filetype'])
  except getopt.GetoptError:
    sys.stderr.write("usage: %s [-f <filetype>] sampleId" % (argv[0],))
    sys.exit(-1)
  for (o,a) in opts:
    if o in ('-f','--filetype'):
      filetype = a
  return args

###############################################################################

args = processOptions(sys.argv)
pp = PrettyPrinter(indent=2,stream=sys.stderr)

proxy = suds.client.Client(wsdlUrl)
slist = proxy.service.getAllSampleLanesForId(arg0=args[0])
## pp.pprint(slist) ## Uncomment to get slots avalilable
try:
  lanes = slist['item']
except TypeError:
  print "error result from 'getAllSampleLanesForId."
  sys.exit(-1)
for lane in lanes:
  run = lane['solexaRun']
  # print "run %d: status=%s,%s" % (run['runNumber'],run['runStatus'],run['analysisStatus'])
  if "COMPLETE" not in run['analysisStatus']:
    print ""
    continue
  id = lane['sampleProcess_id']
  flist = proxy.service.getFileLocations(arg0=id,arg1="FILE",arg2=filetype)
  if flist==None or len(flist)==0:
    print "bad result from 'getFileLocations'."
  else:
    if len(flist['item']) > 0:
      for f in flist['item']:
        ## print "host: %s\npath: %s\nfile: %s\n" % (f['host'],f['path'],f['filename'])
        print('%s:%s' %(f['host'], os.path.join(f['path'],f['filename'])))
    else:
      print "nothing found."
